from win32com import client
import pandas as pd
from docx import Document
import re

# 模板的word文档
word_file_path = r""

# excel数据源
myexcel  = dict()
myexcel['市州汇总']= pd.read_excel(rf'D:\Desktop\统计\市州汇总.xlsx',sheet_name='市州汇总',index_col=0)
myexcel['领域一级汇总']= pd.read_excel(rf'D:\Desktop\统计\领域一级汇总.xlsx',sheet_name='领域一级汇总',index_col=0)
myexcel['领域二级汇总']= pd.read_excel(rf'D:\Desktop\统计\领域二级汇总.xlsx',sheet_name='领域二级汇总',index_col=[0,1])
myexcel['区县汇总']= pd.read_excel(rf'D:\Desktop\统计\区县汇总.xlsx',sheet_name='区县汇总',index_col=[0,1])
myexcel['市州领域一级汇总']= pd.read_excel(rf'D:\Desktop\统计\市州领域一级汇总.xlsx',sheet_name='市州领域一级汇总',index_col=[0,1])
myexcel['市州领域二级汇总']= pd.read_excel(rf'D:\Desktop\统计\市州领域二级汇总.xlsx',sheet_name='市州领域二级汇总',index_col=[0,1,2])
myexcel['市州项目类型汇总']= pd.read_excel(rf'D:\Desktop\统计\市州项目类型汇总.xlsx',sheet_name='市州项目类型汇总',index_col=[0,1])

# 解析的市州
city = '黔南州'

# 打开word解析键值
doc = Document(docx=word_file_path)
keys = list()
for paragraph in doc.paragraphs:
    paragraph_keys = re.findall('{(.*?)}', paragraph.text)
    keys = keys+paragraph_keys

# 汇总所有键值
myfind= set()
for key in keys:
    myfind.add(f'{{{key}}}')
print(myfind)

# 去数据源先将对应键值 找到数据
myvalue=dict()
for n in myfind:
    myn = n[1:len(n)-1]
    arr = myn.split('.')
    if len(arr)==1:
        myvalue[n] = city
    elif len(arr)==2:
        if arr[1].endswith('排序'):
            myvalue[n] = str(myexcel[arr[0]][arr[1]].at[city])
        elif arr[1].endswith('占比'):
            myvalue[n] = f'{round(myexcel[arr[0]][arr[1]].at[city]*100,2)}%'
        else:
            myvalue[n] = str(round(myexcel[arr[0]][arr[1]].at[city],2))
    elif len(arr)==3:
        if arr[2].endswith('排序'):
            myvalue[n] = str(myexcel[arr[0]][arr[2]].at[(city,arr[1])])
        elif arr[2].endswith('占比'):
            myvalue[n] = f'{round(myexcel[arr[0]][arr[2]].at[(city,arr[1])]*100,2)}%'
        else:
            myvalue[n] = str(round(myexcel[arr[0]][arr[2]].at[(city,arr[1])],2))
    elif len(arr)==4:
        if arr[3].endswith('排序'):
            try:
                myvalue[n] = str(myexcel[arr[0]][arr[3]].at[(city,arr[1],arr[2])])
            except:
                myvalue[n] = '0'
        elif arr[3].endswith('占比'):
            try:
                myvalue[n] = f'{round(myexcel[arr[0]][arr[3]].at[(city,arr[1],arr[2])]*100,2)}%'
            except:
                myvalue[n] ='0.00%'
        else:
            try:
                myvalue[n] = str(round(myexcel[arr[0]][arr[3]].at[(city, arr[1], arr[2])], 2))
            except:
                myvalue[n]='0'
    elif len(arr)==5:
        str_all=''
        df = myexcel[arr[0]].xs(city,axis=0)
        for index in df.index:
            str_all=f'{str_all}{index}项目{round(df[arr[1]].at[index],2)}个，年度计划投资{round(df[arr[2]].at[index],2)}亿元；'
        myvalue[n]=f'{str_all[0:len(str_all)-1]}。'
print(myvalue)

# win32找
word = client.Dispatch("Word.Application")
# word.Visible = 0
# word.DisplayAlerts = 0
doc = word.Documents.Open(r"",ReadOnly=False)
r = doc.Range()
for key in myvalue.keys():
    while r.Find.Execute(key) == True:
        print(r.Start, r.End, '\t')
        r.Text = myvalue[key]
        r = doc.Range()

doc.SaveAs(rf"")
doc.Close()
word.Quit()










